package com.zh.blog.security.component;

import cn.hutool.json.JSONUtil;
import com.zh.blog.common.api.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.zh.blog.constant.CommonConst.APPLICATION_JSON;

/**
 * 登录失败处理
 * @author ZhengHao
 * Created At 2022/7/29.
 **/
@Component
public class AuthenticationFailHandlerImpl implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.setContentType(APPLICATION_JSON);
        response.getWriter().write(JSONUtil.toJsonStr(Result.fail(exception.getMessage())));
    }
}